<?php
class Application_Model_Operate extends Application_Model_DomainObject{

    protected $_id; 
    protected $_operate_name; 
    protected $_money; 
    protected $_type;
    protected $_createdate;
    protected $_reid; 
    protected $_content; 
    protected $_userid; 
    protected $_ip; 
    /**
     * 运单受理
     */
    const Type_billaccept = 1;
    /**
     * 运单发货
     */
    const Type_billshipment = 2;
    /**
     * 运单生成转单号
     */
    const Type_billsingle = 3;
    /**
     * 运单发货退款
     */
    const Type_billrefund = 4;
    /**
     * 运单成问题运单
     */
    const Type_billproblem = 5;
    /**
     * 问题运单标记正常
     */
    const Type_billnormal = 6;
    /**
     * 运单生成发票
     */
    const Type_billinvoice = 7;
    /**
     * 修改运单状态
     */
    const Type_billstatus = 8;
    /**
     * 运单取消
     */
    const Type_billcancel = 9;
    /**
     * 运单取消退款
     */
    const Type_billcanre = 10;
    /**
     * 包裹入库
     */
    const Type_instore = 11;
    /**
     * 包裹修改
     */
    const Type_editstore = 12;
    /**
     * 包裹删除
     */
    const Type_delstore = 13;
    /**
     * 代充值支付
     */
    const Type_chargeaccep = 14;
    /**
     * 问题代充值
     */
    const Type_chargeproblem = 15;
    /**
     * 问题代充值退款
     */
    const Type_chargerefund = 16;  
    /**
     * 代支付处理
     */
    const Type_agentprocee = 17;  
    /**
     * 代支付出现问题
     */
    const Type_agentproblem = 18;  
    /**
     * 代支付出现问题退款
     */
    const Type_agentrefund = 19;  
    /**
     * 修改用户信息
     */
    const Type_edituser = 20;
    /**
     * 重置用户密码
     */
    const Type_userrepwd = 21;
    /**
     * 用户添加备注
     */
    const Type_addremark = 22;
    /**
     * 手工充值
     */
    const Type_rechargeuser = 23;
    /**
     * 手工扣款
     */
    const Type_deductuser = 24;
    /**
     * 添加优惠券
     */
    const Type_addcoupon = 25;
    /**
     * 添加优惠券批次
     */
    const Type_addbatch = 26;
    /**
     * 更改代购的状态
     */
    const Type_editbuyshop = 27;
    /**
     * 给商品添加备注
     */
    const Type_addcomment = 28;
    /**
     * 代购商品单项退款
     */
    const Type_singrefund = 29;
    /**
     * 代购商品单项运费退款
     */
    const Type_shipFeerefund = 30;
    /**
     * 代购订单全部退款
     */
    const Type_buyshoprefund = 31;
    /**
     * 更新代购订单物流信息
     */
    const Type_addlogistics = 32;
    /**
     * 更新代购订单编号
     */
    const Type_addprocureNo = 33;
    /**
     * 问题运单发送站内信
     */
    const Type_billmessage = 34;
    /**
     * 问题运单发送邮件
     */
    const Type_billmail = 35;
    /**
     * 问题代充值发送站内信
     */
    const Type_chargemessage = 36;
    /**
     * 问题代充值发送邮件
     */
    const Type_chargemail = 37;
    /**
     * 问题代支付发送站内信
     */
    const Type_agentmessage = 38;
    /**
     * 问题代支付发送邮件
     */
    const Type_agentmail = 39;
    /**
     * 手工充值发送站内信
     */
    const Type_rechargemessage = 40;
    /**
     * 手工扣款发送站内信
     */
    const Type_deductmessage = 41;
    /**
     * 手工添加站内信
     */
    const Type_relmessage = 42;
    /**
     * 手工发起运单
     */
    const Type_addbill = 43;
    /**
     * 手工修改用户收件人地址
     */
    const Type_editaddress = 44;
    /**
     * 删除用户收件人地址
     */
    const Type_deladdress = 45;
    /**
     * 修改后台用户信息
     */
    const Type_editadminuser = 46;
    /**
     * 添加后台用户信息
     */
    const Type_addadminuser = 47;
    /**
     * 删除后台用户
     */
    const Type_deladminuser = 48;
    /**
     * 修改淘宝api 
     */
    const Type_edittaobaoapi = 49;
    /**
     * 添加淘宝api 
     */
    const Type_addtaobaoapi = 50;
    /**
     * 删除淘宝api 
     */
    const Type_deltaobaoapi = 51;
    /**
     * 查看待支付
     */
    const Type_chargedetail = 52;
    /*
     * 运单交运
     */
    const Type_AffirmChannel = 53;

    public static function intooperate($money,$type,$reid,$userid,$status = null)
    {
        $operateItem = new Application_Model_Operate();
        $operateItem->operate_name = Uuch_Util::decryptString($_COOKIE["__adminusername"]);
        $operateItem->money = $money;
        $operateItem->type = $type;
        $operateItem->createdate = date("Y-m-d H:i:s");
        $operateItem->reid = $reid;
        if($type == Application_Model_Operate::Type_billaccept){
            $operateItem->content = "国际运单 " . $reid . " 受理";
        }else if($type == Application_Model_Operate::Type_billshipment){
            $operateItem->content = "国际运单 " . $reid . " 发货";
        }else if($type == Application_Model_Operate::Type_billsingle){
            $operateItem->content = "国际运单 " . $reid . " 生成转单号";
        }else if($type == Application_Model_Operate::Type_billrefund){
            $operateItem->content = "国际运单 " . $reid . " 发货退款";
        }else if($type == Application_Model_Operate::Type_billproblem){
            $operateItem->content = "国际运单 " . $reid . " 出现问题";
        }else if($type == Application_Model_Operate::Type_billnormal){
            $operateItem->content = "问题国际运单 " . $reid . " 标记正常";
        }else if($type == Application_Model_Operate::Type_AffirmChannel){
            $operateItem->content = "国际运单 " . $reid . " 交运";
        }else if($type == Application_Model_Operate::Type_billinvoice){
            $operateItem->content = "国际运单 " . $reid . " 修改发票";
        }else if($type == Application_Model_Operate::Type_billstatus){
            $operateItem->content = "修改国际运单 " . $reid . " 的状态为". $status;
        }else if($type == Application_Model_Operate::Type_billstatus){
            $operateItem->content = "修改国际运单 " . $reid . " 取消";
        }else if($type == Application_Model_Operate::Type_billcanre){
            $operateItem->content = "修改国际运单 " . $reid . " 取消退款";
        }else if($type == Application_Model_Operate::Type_instore){
            $operateItem->content = "包裹 " . $reid . " 入库";
        }else if($type == Application_Model_Operate::Type_editstore){
            $operateItem->content = "包裹 " . $reid . " 修改信息";
        }else if($type == Application_Model_Operate::Type_delstore){
            $operateItem->content = "包裹 " . $reid . " 删除";
        }else if($type == Application_Model_Operate::Type_chargeaccep){
            $operateItem->content = "代充值 " . $reid . " 充值";
        }else if($type == Application_Model_Operate::Type_chargeproblem){
            $operateItem->content = "代充值 " . $reid . " 出现问题";
        }else if($type == Application_Model_Operate::Type_chargerefund){
            $operateItem->content = "代充值 " . $reid . " 出现问题退款";
        }else if($type == Application_Model_Operate::Type_agentprocee){
            $operateItem->content = "代支付 " . $reid . " 支付，支付金额为：".$status;
        }else if($type == Application_Model_Operate::Type_agentproblem){
            $operateItem->content = "代支付 " . $reid . " 出现问题";
        }else if($type == Application_Model_Operate::Type_agentrefund){
            $operateItem->content = "代支付 " . $reid . " 出现问题退款";
        }else if($type == Application_Model_Operate::Type_edituser){
            $operateItem->content = "用户 " . $reid . " 信息修改";
        }else if($type == Application_Model_Operate::Type_userrepwd){
            $operateItem->content = "用户 " . $reid . " 重置密码";
        }else if($type == Application_Model_Operate::Type_addremark){
            $operateItem->content = "用户 " . $reid . " 添加备注";
        }else if($type == Application_Model_Operate::Type_rechargeuser){
            $operateItem->content = "用户 " . $reid . " 手工充值";
        }else if($type == Application_Model_Operate::Type_deductuser){
            $operateItem->content = "用户 " . $reid . " 手工扣款";
        }else if($type == Application_Model_Operate::Type_addcoupon){
            $operateItem->content = "添加优惠券 " . $reid ;
        }else if($type == Application_Model_Operate::Type_addbatch){
            $operateItem->content = "添加优惠券批次 " . $reid ;
        }else if($type == Application_Model_Operate::Type_editbuyshop){
            $operateItem->content = "修改代购运单 " . $reid . " 的状态为". $status;
        }else if($type == Application_Model_Operate::Type_addcomment){
            $operateItem->content = "代购运单 " . $reid . " 的添加备注";
        }else if($type == Application_Model_Operate::Type_singrefund){
            $operateItem->content = "代购运单 " . $reid . " 单项退款";
        }else if($type == Application_Model_Operate::Type_shipFeerefund){
            $operateItem->content = "代购运单 " . $reid . " 单项运费退款";
        }else if($type == Application_Model_Operate::Type_buyshoprefund){
            $operateItem->content = "代购运单 " . $reid . " (包含运费)全部退款";
        }else if($type == Application_Model_Operate::Type_addlogistics){
            $operateItem->content = "代购运单 " . $reid . " 物流信息更新";
        }else if($type == Application_Model_Operate::Type_addprocureNo){
            $operateItem->content = "代购运单 " . $reid . " 代购编号更新";
        }else if($type == Application_Model_Operate::Type_billmessage){
            $operateItem->content = "问题国际运单 " . $reid . " 发送站内信";
        }else if($type == Application_Model_Operate::Type_billmail){
            $operateItem->content = "问题国际运单 " . $reid . " 发送邮件";
        }else if($type == Application_Model_Operate::Type_chargemessage){
            $operateItem->content = "问题代充值 " . $reid . "发送站内信";
        }else if($type == Application_Model_Operate::Type_chargemail){
            $operateItem->content = "问题代充值 " . $reid . " 发送邮件";
        }else if($type == Application_Model_Operate::Type_agentmessage){
            $operateItem->content = "问题代支付 " . $reid . " 发送站内信";
        }else if($type == Application_Model_Operate::Type_agentmail){
            $operateItem->content = "问题代支付" . $reid . " 发送邮件";
        }else if($type == Application_Model_Operate::Type_rechargemessage){
            $operateItem->content = "会员 " . $reid . " 手工充值发送站内信";
        }else if($type == Application_Model_Operate::Type_deductmessage){
            $operateItem->content = "会员 " . $reid . " 手工扣款发送站内信";
        }else if($type == Application_Model_Operate::Type_relmessage){
            $operateItem->content = "会员 " . $userid . " 手工发送站内信";
        }else if($type == Application_Model_Operate::Type_addbill){
            $operateItem->content = "给会员 " . $userid . " 手工发起运单（".$reid."）";
        }else if($type == Application_Model_Operate::Type_editaddress){
            $operateItem->content = "给会员 " . $userid . " 修改收件人地址（".$reid."）";
        }else if($type == Application_Model_Operate::Type_deladdress){
            $operateItem->content = "给会员 " . $userid . " 删除收件人地址（".$reid."）";
        }else if($type == Application_Model_Operate::Type_editadminuser){
            $operateItem->content = "修改后台用户 " . $userid . " 的信息";
        }else if($type == Application_Model_Operate::Type_addadminuser){
            $operateItem->content = "添加后台用户 " . $userid;
        }else if($type == Application_Model_Operate::Type_deladminuser){
            $operateItem->content = "删除后台用户 " . $userid;
        }else if($type == Application_Model_Operate::Type_edittaobaoapi){
            $operateItem->content = "修改淘宝api " . $reid." 信息";
        }else if($type == Application_Model_Operate::Type_addtaobaoapi){
            $operateItem->content = "添加淘宝api " . $reid;
        }else if($type == Application_Model_Operate::Type_deltaobaoapi){
            $operateItem->content = "删除淘宝api " . $reid;
        }else if($type == Application_Model_Operate::Type_chargedetail){
            $operateItem->content = "查看代支付 " . $reid." 的详情";
        }
        $ip = Application_Model_Operate::get_local_ip();
        $operateItem->ip = $ip;
        $operateItem->userid = $userid;
        $operateItem->save();
        return TRUE;
    }
    
    public static function getserFilter($quick,$dateinbegin,$dateinend,$pagesize, $pageindex)
    {
        $where = array();
        $and = "";
        if($quick != ""){
            $and = " ( ";
            $where[$and." operate_name like "] = "%".$quick."%";
            $and = " or ";
            $where[$and." reid like "] = "%".$quick."%";
            $where[$and." content like "] = "%".$quick."%";
            $where[$and." userid like "] = "%".$quick."%";
            $uwhere[" storeno like "] = "%".$quick."%";
            $userItem = Application_Model_User::getDao()->queryAll($uwhere);
            if($userItem){
                $userid = "(";
                foreach ($userItem as $uvalue) {
                    $userid .= $uvalue->id.",";
                }
                $userid .= "0)";
                $where[$and." userid in "] = $userid;
            }
            $where[" ) and 1 = "] = 1;
            $and = " and ";
        }
        if($dateinbegin != ""){
            $where[$and." createdate >= "] =$dateinbegin;
            $and = " and ";
        }
        if($dateinend != ""){
            $where[$and." createdate <= "] =$dateinend;
            $and = " and ";
        }
        return Application_Model_Operate::getDao()->queryPage($where, " createdate desc ", $pagesize, $pageindex);
    }
    
    public function getuserde()
    {
        if($this->userid){
            return Application_Model_User::load($this->_userid);
        }
    }
    public static function get_local_ip()
    {
        $arr = "";
        exec("ipconfig /all",$arr); //运行这句需要修改php.ini文件并重启apache
	if (is_array($arr)){
            foreach ($arr AS $val) {
                if (preg_match("/IPv4/", $val)) {
                    $pip = substr($val, strpos($val, ":") + 1);
                    if (preg_match("/192\.168\.1\./", $pip)) {  
                        $pip = explode("(",$pip);
                        return $pip[0];
                    }
                    if (preg_match("/192\.168\.2\./", $pip)) {  
                        $pip = explode("(",$pip);
                        return $pip[0];
                    }
                }
            }
	}
    }
}